<?php
// declare(strict_types=1); // 开启严格模式
namespace app\common\library;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use think\Exception;

class AliSms
{
    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dysmsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([
            // 您的 AccessKey ID
            "accessKeyId" => config('app.aliyun.accessKeyId'),
            // 您的 AccessKey Secret
            "accessKeySecret" => config('app.aliyun.accessKeySecret'),
        ]);
		// halt(config('app.aliyun.accessKeyId'));
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";//固定的
        return new Dysmsapi($config);
    }


    /**
	 * @param string[] $args
	 * @return array
	 */
	public static function main($args,$type){
		$client = self::createClient("accessKeyId", "accessKeySecret");
		// halt($client);
		// 禁用 SSL 证书验证
		// $client->options([
		// 	'verify' => false,
		// ]);
		if($type == 1){
			//发送的是验证码
			$sendSmsRequest = new SendSmsRequest([
				"phoneNumbers" => $args['phone'],  //发送的手机号码
				// "signName" => "健康管家会员中心",  //签名
				"signName" => $args['name'],  //签名
				"templateCode" => $args['template'],  //模板CODE
				"templateParam" => json_encode($args['info'])  //这个根据你的模板信息来
			]);
		}else{
			//发送的是营销短信
			$sendSmsRequest = new SendSmsRequest([
				"phoneNumbers" => $args['phone'],  //发送的手机号码
				"signName" => $args['name'],  //签名
				"templateCode" => $args['template'],  //模板CODE
				// "templateParam" => json_encode($args['info'])  //这个根据你的模板信息来
			]);
		}
		
		// $re['event'] = $event;
		// $re['mobile'] = $mobile;
		// $re['code'] = $code;
		// $re['type'] = $type;
		// $re['ip'] = request()->ip();
		// $re['coupon_code_psd'] = $order_title;
		// $re['content'] = json_encode($result);
		// $re['createtime'] = time();
		// $re['status'] = $result['result'];
		
		// Db::name('error_log')->insert($re);
		
		try{
		    $result = $client->sendSms($sendSmsRequest);
			$result = json_decode(json_encode($result),true);
			// halt($result);
			if ($result['body']['message'] == 'OK' && $result['body']['code'] == 'OK') {
				// echo 1;
				return ['result' => 0, 'message' => '发送成功'];
			}else{
				return ['result' => 1001, 'message' => $result['body']['message']];
			}
		}catch(\Exception $e){
		    $pars['title'] = '阿里云短信接口异常';
		    $pars['content'] = $e->getMessage();
		    $result = request_post('http://cladmin.xinquanyu.top/index.php/api/Apimobile/sendDing',json_encode($pars));
		    return ['result' => 1001, 'message' => '接口调用失败'];
		}
		
		// if ($result['body']['code'] == 'isv.MOBILE_NUMBER_ILLEGAL') {
		// 	// echo 2;
		// 	return ['code' => 1001, 'message' => '手机号码格式不正确'];
		// }
		// if ($result['body']['code'] == 'isv.BUSINESS_LIMIT_CONTROL') {
		// 	return ['code' => 200, 'message' => '触发小时级流控Permits:2'];
		// }
		// return ['code' => 1002, 'message' => '短信发送失败，网络繁忙'];
	}
}
